package com.zn.websocket.handler;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zn.util.JSONUtil;
import com.zn.websocket.group.ChannelGroups;
import com.zn.websocket.vo.WebSocketSendVo;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * WebSocket请求处理类
 * @version 0.1
 * @time 12/19/17
 * @since 0.1
 */
@Sharable
@Component
public class WebSocketHandler extends ChannelInboundHandlerAdapter {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketHandler.class);

    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final ObjectMapper OBJECT_MAPPER ;
    static {
        OBJECT_MAPPER = new ObjectMapper();
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        LOGGER.warn("channel close...........");
        ChannelGroups.discard(ctx.channel());
    }

//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        if (msg instanceof WebSocketFrame) {
//            if (msg instanceof TextWebSocketFrame) {
//                TextWebSocketFrame frame = ((TextWebSocketFrame) msg);
//                String uuid = frame.text();
//                LOGGER.warn("uuid........." + uuid);
//                //将用户token进行对channel的绑定
//                if(StringUtils.isNotBlank(uuid)){
//                    ChannelGroups.bindChannel(uuid,ctx.channel());
//                }
//            } else if (msg instanceof CloseWebSocketFrame) {
//                ctx.close();
//            }
//            WebSocketFrame wsf = (WebSocketFrame) msg;
//            if (wsf.refCnt() == 1)
//                wsf.release();
//        }
//    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof WebSocketFrame) {
            if (msg instanceof TextWebSocketFrame) {
                TextWebSocketFrame frame = ((TextWebSocketFrame) msg);

                String context = frame.text();
                WebSocketSendVo webSocketSendVo=(WebSocketSendVo)JSONUtil.JSONToObj(context,WebSocketSendVo.class);
                if(webSocketSendVo!=null){
                    LOGGER.warn("content........." + webSocketSendVo.toString());
                    if(StringUtils.isNotBlank(webSocketSendVo.getCustomerId())){
                        //绑定单个webSocekt通道
                        ChannelGroups.bindChannel(webSocketSendVo.getCustomerId(),ctx.channel());
                    }
                    if(StringUtils.isNotBlank(webSocketSendVo.getInstrumentMonth())){
                        //将发送的月份绑定
                        ChannelGroups.bindChannel(webSocketSendVo.getUuid(),webSocketSendVo.getInstrumentMonth(),ctx.channel());
                    }
                }else{
                    LOGGER.error("WebSocketSendVo is null...................");
                }
            } else if (msg instanceof CloseWebSocketFrame) {
                ctx.close();
            }
            WebSocketFrame wsf = (WebSocketFrame) msg;
            if (wsf.refCnt() == 1)
                wsf.release();
        }
    }

}
